using System.Windows.Markup;
using Avalonia.Markup.Xaml;
using Avalonia.Markup.Xaml.Styling;
using Avalonia.Styling;
using KinonekoSoftware.CodariaDB.Documents.Templates;
using KinonekoSoftware.Foundation.UI.Controls;
using KinonekoSoftware.Foundation.UI.Converters;

namespace KinonekoSoftware.Studio.Resources.Converters
{

    public class FindDataPartGeometryConverter : ConverterBase
    {
        private static readonly Dictionary<DataPartType, string> _IconDictionary = new Dictionary<DataPartType, string>();

        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DataPartTypeSelector dptS)
            {
                return Application.Current!.Resources?[dptS.Icon] as Geometry;
            }

            if (value is not DataPartType type)
            {
                type = DataPartType.Basic_Custom;
            }

            if (!_IconDictionary.TryGetValue(type, out var icon))
            {
                icon = DataPart.GetIconName(type);

                if (string.IsNullOrEmpty(icon))
                {
                    return null;
                }

                _IconDictionary.Add(type, icon);
            }


            return Application.Current!.Resources.TryGetResource(icon, ThemeVariant.Default, out var r) ? r as Geometry : null;
        }
    }
}